//
//  BaseNavigationController.m
//  YOYOLive
//
//  Created by 王根强 on 2016/10/10.
//  Copyright © 2016年 王根强. All rights reserved.
//

#import "BaseNavigationController.h"

@implementation BaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forBarMetrics:UIBarMetricsDefault];
//    [[UINavigationBar appearance] setBackgroundImage:[UIImage imageWithColor:NAV_COLOR_BLUE andSize:CGSizeMake(1, 1)] forBarMetrics:UIBarMetricsDefault];
    [self.navigationBar setTitleTextAttributes:
     @{NSFontAttributeName:[UIFont boldSystemFontOfSize:20],
       NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationBar.hidden = YES;
}

- (id)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        self.view.backgroundColor = [UIColor whiteColor];
        _backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style: UIBarButtonItemStylePlain target: nil action: nil];
        _backBarButtonItem.title = @"";
        rootViewController.navigationItem.backBarButtonItem = _backBarButtonItem;
    }
    return self;
}

//overload push  method
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    //统一修改返回按钮
    if (self.viewControllers.count >= 1) {
        UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [backBtn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
        backBtn.frame = CGRectMake(0, 0, 44, 44);
        backBtn.imageEdgeInsets = UIEdgeInsetsMake(0,-24, 0, 0);
        [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
        viewController.navigationItem.leftBarButtonItem = backBarButtonItem;
    }
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)backAction {
    [self popViewControllerAnimated:NO];
}

@end

//改变状态栏颜色为白色
@interface UINavigationController (StatusBarStyle)

@end

@implementation UINavigationController (StatusBarStyle)

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}
@end
